﻿@using System.Text
@model ShipmentModel
@{
    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderId);
    
    var itemsFromMultipleWarehouses = Model.Items
        .Where(x => x.AllowToChooseWarehouse && x.AvailableWarehouses.Count > 0)
        .ToList();
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-sales.png")" alt="" />
            @string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderId) @Html.ActionLink("(" + T("Admin.Orders.Shipments.BackToOrder") + ")", "Edit", new { id = Model.OrderId })
        </div>
        <div class="options">
            <input type="submit" id="shipment-save" name="save" class="k-button" value="@T("Admin.Common.Save")" />
            <input type="submit" id="shipment-save-continue" name="save-continue" class="k-button" value="@T("Admin.Common.SaveContinue")" />
        </div>
    </div>    
    @Html.ValidationSummary(false)
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.TrackingNumber):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.TrackingNumber)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AdminComment):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AdminComment)
            </td>
        </tr>
    </table>
    if (itemsFromMultipleWarehouses.Any())
    {
        <script type="text/javascript">
            $(document).ready(function () {
                $('#shipment-save').click(function () {
                    return validateWarehouseAvailability();
                });
                $('#shipment-save-continue').click(function () {
                    return validateWarehouseAvailability();
                });
            });
            

            function validateWarehouseAvailability() {
                @{
                    var sb = new StringBuilder();
                    for (int i = 0; i <= itemsFromMultipleWarehouses.Count - 1; i++)
                    {
                        var item = itemsFromMultipleWarehouses[i];
                        sb.AppendFormat("validateAvailableQuantity('{0}')", item.OrderItemId);
                        if (i != itemsFromMultipleWarehouses.Count - 1)
                        {
                            sb.Append(" && ");
                        }
                    }
                }
                var valid = @(Html.Raw(sb.ToString()));
                if (!valid) {
                    return confirm('@T("Admin.Orders.Shipments.Products.Warehouse.QuantityNotEnough")');
                }
                return true;
            }
                
            function validateAvailableQuantity(orderItemId) {
                var enteredValue = parseInt($('#qtyToAdd' + orderItemId).val(), 10);
                if (enteredValue <= 0)
                    return true;
                var reservedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('reserved-qty'), 10);
                var plannedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('planned-qty'), 10);
                var availableToAdd = reservedValue - plannedValue;
                return enteredValue <= availableToAdd;
            }
        </script>
    }
    <table class="adminContent">
        <tr>
            <td class="adminData">
                <strong>@T("Admin.Orders.Shipments.Products")</strong>
            </td>
        </tr>
        <tr>
            <td class="adminData">
                <table class="tablestyle" border="1" cellspacing="0" style="width: 100%; border-collapse: collapse;">
                    <thead>
                        <tr class="headerstyle">
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.ProductName")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.SKU")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.Warehouse")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.ItemWeight")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.ItemDimensions")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.QtyOrdered")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.QtyShipped")
                            </th>
                            <th align="center">
                                @T("Admin.Orders.Shipments.Products.QtyToShip")
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Items)
                        {
                            <tr>
                                <td style="width:30%;">
                                    <div style="padding-left:10px;padding-right:10px;">
                                        <em><a href="@Url.Action("Edit", "Product", new {id = item.ProductId})">@item.ProductName</a></em>
                                        @if (!String.IsNullOrEmpty(item.AttributeInfo))
                                        {
                                            <br />
                                            @Html.Raw(item.AttributeInfo)
                                        }
                                        @if (item.ShipSeparately)
                                        {
                                            <br />
                                            <strong>@T("Admin.Orders.Shipments.Products.ShipSeparately.Warning")</strong>
                                            @T("Admin.Orders.Shipments.Products.ShipSeparately")
                                        }
                                    </div>
                                </td>
                                <td style="width:10%;">
                                    <div style="padding-left:10px;padding-right:10px;">
                                        @item.Sku
                                    </div>
                                </td>
                                <td align="center" style="width:20%;">
                                    <div style="padding-left:10px;padding-right:10px;">
                                        @if (item.AllowToChooseWarehouse)
                                        {
                                            if (item.AvailableWarehouses.Count > 0)
                                            {
                                                <select name="warehouse_@(item.OrderItemId)" id="warehouse_@(item.OrderItemId)">
                                                    @foreach (var warehouse in item.AvailableWarehouses)
                                                    {
                                                        <option value="@warehouse.WarehouseId" data-stock-qty="@warehouse.StockQuantity" data-reserved-qty="@warehouse.ReservedQuantity" data-planned-qty="@warehouse.PlannedQuantity">
                                                            @T("Admin.Orders.Shipments.Products.Warehouse.ChooseQty", warehouse.WarehouseName, warehouse.StockQuantity, warehouse.ReservedQuantity, warehouse.PlannedQuantity)
                                                        </option>
                                                    }
                                                </select>
                                            }
                                            else
                                            {
                                                <div>@T("Admin.Orders.Shipments.Products.Warehouse.NotAvailabe")</div>
                                            }
                                        }
                                        else
                                        {
                                            //display warehouses
                                            for (int i = 0; i < item.AvailableWarehouses.Count; i++)
                                            {
                                                var warehouse = item.AvailableWarehouses[i];
                                                @warehouse.WarehouseName
                                                if (i != item.AvailableWarehouses.Count - 1)
                                                {
                                                    <br />
                                                }
                                            }
                                        }
                                    </div>
                                </td>
                                <td align="center" style="width:10%;">
                                    @item.ItemWeight
                                </td>
                                <td align="center" style="width:10%;">
                                    @item.ItemDimensions
                                </td>
                                <td align="center" style="width:5%;">
                                    @item.QuantityOrdered
                                </td>
                                <td align="center" style="width:5%;">
                                    @item.QuantityInAllShipments
                                </td>
                                <td align="center" style="width:20%;">
                                    <input name="qtyToAdd@(item.OrderItemId)" type="text" value="@item.QuantityToAdd" id="qtyToAdd@(item.OrderItemId)" class="adminInput" style="width:100px;" />
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
}